$(function () {

    let user_search_content = $.session.get('search_content')
    console.log(user_search_content);
    $("#search_content").val(user_search_content);
    let pageNum = 1
    let max_data
    min_search_content_function()
    $("#search_button_main_in_search_page").click(function () {
        $(".feed-box").empty();
        user_search_content = $("#search_content").val();
        min_search_content_function()
    });

    function min_search_content_function() {
        if (user_search_content != null && user_search_content != "") {
            all_search_content('headlines')
            all_search_content('article')
            all_search_content('imgtext')
            all_search_content('lvideo')
            all_search_content('svideo')
        }
    }

    function all_search_content(type) {
        $.ajax({
            url: baseURL + '/webapi/works/searchAllWorksPage',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                wd: user_search_content,
                type: type,
                pageNum: pageNum,
                pageSize: 20,
            },
            async: false,//同步
            success: function (data) {
                console.log(data);
                main_search_content(data)
            }
        })
    }

    function other_search_content(type) {
        if (user_search_content != null && user_search_content != "") {
            $.ajax({
                url: baseURL + '/webapi/works/searchAllWorksPage',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    wd: user_search_content,
                    type: type,
                    pageNum: pageNum,
                    pageSize: 20,
                },
                async: false,//同步
                success: function (data) {
                    console.log(data);
                    let data_content
                    if (data != null && data != "") {
                        data_content = data.data
                    }
                    console.log(data_content);
                    if (data_content != null && data_content != "") {
                        if (data_content.length != null && data_content.length != "") {
                            max_data = data_content.length
                            if (max_data >= 20) {
                                max_data = 20
                            }
                        }
                    }
                    for (let i = 0; i < max_data; i++) {
                        if (type == 'headlines') {
                            headlines(data_content[i])
                        } else if (type == 'article') {
                            article(data_content[i])
                        } else if (type == 'imgtext') {
                            imgtext(data_content[i])
                        } else if (type == 'lvideo') {
                            video(data_content[i])
                        } else if (type == 'svideo') {
                            video(data_content[i])
                        }
                    }
                }
            })
        }
    }

    function main_search_content(data) {
        console.log(data);
        let data_content
        if (data != null && data != "") {
            data_content = data.data
        }
        console.log(data_content);
        if (data_content != null && data_content != "") {
            if (data_content.length != null && data_content.length != "") {
                max_data = data_content.length
                if (max_data >= 20) {
                    max_data = 20
                }
                for (let i = 0; i < max_data; i++) {
                    console.log(data_content[i].type);
                    if (data_content[i].type == 'headlines') {
                        headlines(data_content[i])
                    } else if (data_content[i].type == 'article') {
                        article(data_content[i])
                    } else if (data_content[i].type == 'imgtext') {
                        imgtext(data_content[i])
                    } else if (data_content[i].type == 'lvideo') {
                        video(data_content[i])
                    } else if (data_content[i].type == 'svideo') {
                        video(data_content[i])
                    }
                }
            }
        }
    }

    //分类页面切换
    $(".tab-item").on("click", function () {
        $(".tab-item").removeClass("active");
        $(this).addClass("active");

    });
    $(".all").on("click", function () {
        $(".feed-box").empty();
        min_search_content_function()
    });
    $(".Headlines").on("click", function () {
        $(".feed-box").empty();
        other_search_content('headlines')
    });
    $(".article").on("click", function () {
        $(".feed-box").empty();
        other_search_content('article')

    });
    $(".img-text").on("click", function () {
        $(".feed-box").empty();
        other_search_content('imgtext')

    });
    $(".video").on("click", function () {
        $(".feed-box").empty();
        other_search_content('lvideo')

    });
    $(".micro-video").on("click", function () {
        $(".feed-box").empty();
        other_search_content('svideo')

    });
    $(".collection").on("click", function () {
        $(".feed-box").empty();

    });

    // 方法
    function headlines(data) {
        console.log(data);
        let img_url = getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0)
        if (img_url == 'images/index/视角LOGO 背景.jpg?x-oss-process=image/resize,w_500') {
            $(".feed-box").append(
                '            <div class="post-list-item col-sm-12 "uid="' + data.id + '">\n' +
                '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 15px">\n' +
                '                    <a href="news.html">\n' +
                '                        <div class="post-heading link" uid="' + data.data.id + '">' + data.data.title + '</div>\n' +
                '                    </a>\n' +
                '                    <p class="">\n' +
                '                        <a href="other_user.html"><span>\n' +
                '                            <img class="avatar"\n' +
                '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                '                        ' + data.uname + '\n' +
                '                        </span> ⋅</a>\n' +
                '                        <span>\n' +
                '                        ' + data.commentCount + '评论\n' +
                '                        </span> ⋅\n' +
                '                        <span>\n' +
                // '                                ' + time(data.atime) + '\n' +
                '                            </span>\n' +
                '                    </p>\n' +
                '                </div>\n' +
                '            </div>'
            );
        } else {
            $(".feed-box").append(
                '            <div class="post-list-item col-sm-12 no_padding_left"uid="' + data.id + '">\n' +
                '                <div class="head_img fl ">\n' +
                '                    <img class="title_head_img user_img_item" src="' + getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0) + '" alt="">\n' +
                '                </div>\n' +
                '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 15px">\n' +
                '                    <a href="news.html">\n' +
                '                        <div class="post-heading link" uid="' + data.id + '">' + data.title + '</div>\n' +
                '                    </a>\n' +
                '                    <p class="">\n' +
                '                        <a href="other_user.html"><span>\n' +
                '                            <img class="avatar"\n' +
                '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                '                        ' + data.uname + '\n' +
                '                        </span> ⋅</a>\n' +
                '                        <span>\n' +
                '                        ' + data.commentCount + '评论\n' +
                '                        </span> ⋅\n' +
                '                        <span>\n' +
                // '                                ' + time(data.atime) + '\n' +
                '                            </span>\n' +
                '                    </p>\n' +
                '                </div>\n' +
                '            </div>'
            );
        }
    }

    function article(data) {
        $(".feed-box").append(
            '<div class="no-mode col-sm-12" uid="' + data.id + '">\n' +
            '    <div class="title-box col-sm-12"><a class="link"  type="news"  uid="' + data.id + '">' + data.title + '</a></div>\n' +
            '    <div class="footer-bar col-sm-12">\n' +
            '        <div class="footer-bar-left col-sm-10">\n' +
            '            <a class="footer-bar-action tag tag-style-video" href="video.html">视频</a>\n' +
            '            <a class="footer-bar-action media-avatar" href="other_user.html"><img\n' +
            '                    src="images/index/logo.png" alt=""></a>\n' +
            '            <a class="footer-bar-action source" href="other_user.html"> ' + data.uname + '·&nbsp; </a>\n' +
            '            <a class="footer-bar-action source" href="video.html"> ' + data.commentCount + '评论·&nbsp;</a>\n' +
            '            <span class="footer-bar-action source time">' + time(data.atime) + '</span>\n' +
            '        </div>\n' +
            '        <div class="footer-bar-right col-sm-2"></div>\n' +
            '    </div>\n' +
            '</div>'
        );
    }

    function imgtext(data) {
        $(".feed-box").append(
            '<div class="ugc-mode col-sm-12" uid="' + data.id + '">\n' +
            '    <div class="col-xs-3 ugc-mode-lbox" style="padding-left: 0">\n' +
            '        <a class="img-wrap" href="news.html">\n' +
            '            <img class="post-object lazy-load-img "  \n' +
            '                 src="' + getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0) + '">\n' +
            '        </a>\n' +
            '    </div>\n' +
            '    <div class="col-sm-9 ugc-mode-right ugc-mode-rbox" style="padding-left: 0">\n' +
            '        <div class="col-sm-12 ugc-mode-rbox-inner" style="margin-top: 5px">\n' +
            '            <div class="ugc-mode-user">\n' +
            '                <a href="other_user.html"><img class="ugc-avatar" src="images/index/中国网.jpg" alt=""></a>\n' +
            '                <div class="ugc-desc">\n' +
            '                    <a class="ugc-name" href="other_user.html">\n' +
            '                        <span>' + data.uname + '</span>\n' +
            '                        <span class="dv-icon" style=";"></span>\n' +
            '                        <i class="bui-icon icon-vtt"></i>\n' +
            '                    </a>\n' +
            '                    <p class="ugc-meta">\n' +
            '                        <span>' + data.signature + '</span>\n' +
            '                    </p>\n' +
            '                </div>\n' +
            '            </div>\n' +
            '            <div class="ugc-mode-content">\n' +
            '                <a href="news.html" class="link" uid="' + data.id + '">' + data.title + '</a>\n' +
            '            </div>\n' +
            '            <div class="ugc-mode-footer">\n' +
            '                <div class="footer-bar-left">\n' +
            '                    <a class="footer-bar-action source" href="other_user.html"> ' + data.uname + '·&nbsp; </a>\n' +
            '                    <a class="footer-bar-action source" href="news.html"> ' + data.commentCount + '评论·&nbsp;</a>\n' +
            '                    <span class="footer-bar-action source time">' + time(data.atime) + '</span>\n' +
            '                </div>\n' +
            '                <div class="footer-bar-right">\n' +
            '\n' +
            '                </div>\n' +
            '            </div>\n' +
            '        </div>\n' +
            '    </div>\n' +
            '</div>\n'
        );
    }

    function video(data) {
        $(".feed-box").append(
            '            <div class="post-list-item col-sm-12"style="padding-left: 0;" type="'+data.type+'" uid="' + data.id + '">\n' +
            '                <div class="head_img fl ">\n' +
            '                    <img class="title_head_img link" type="'+data.type+'" uid="' + data.id + '" src="' + getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0) + '" alt="">\n' +
            '                </div>\n' +
            '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 0">\n' +
            '                    <a href="video.html">\n' +
            '                        <div class="post-heading link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</div>\n' +
            '                    </a>\n' +
            '                    <p class="">\n' +
            '                        <a href="video.html"><span>\n' +
            '                            <img type="'+data.type+'" uid="' + data.id + '" class="avatar link"\n' +
            '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
            '                        ' + data.uname + '\n' +
            '                        </span> ⋅</a>\n' +
            '                        <span>\n' +
            '                        ' + data.commentCount + '评论\n' +
            '                        </span> ⋅\n' +
            '                        <span>\n' +
            '                                ' + time(data.atime) + '\n' +
            '                            </span>\n' +
            '                    </p>\n' +
            '                </div>\n' +
            '            </div>'
        );
    }


    //时间截取方法
    function time(time) {
        arr = (time || "").split('T');
        return arr[0]
    }

//jq end
})